
package com.yishe.config.db;


import com.alibaba.druid.pool.DruidDataSource;
import com.yishe.constant.DatabaseDialect;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * create by: chusifeng
 * Create in 2021/4/9 13:49
 * Description:mybatis 配置数据源类
 * <p>Example</p>
 */
@Slf4j 
public class MySQLDataSourceConfig extends DataSourceConfig {
	/** mysql 驱动名称，默认值为：com.mysql.jdbc.Driver  **/
	@Value(value = "${spring.datasource.mysql.driver-class-name:com.mysql.jdbc.Driver}")
	private String driveClassName;
	/** mysql 连接地址  **/
	@Value(value = "${spring.datasource.mysql.url:mysql-url-unknown}")
	private String url;
	/** mysql 用户名称  **/
	@Value(value = "${spring.datasource.mysql.username:mysql-username-unknown}")
	private String userName;
	/** mysql 用户密码 （加密模式） **/
	@Value(value = "${spring.datasource.mysql.password:mysql-password-unknown}")
	private String password;
	/** mysql 扫描xml目录  **/
	@Value(value = "${mybatis.mysql.xmlLocation:mysql-unknown}")
	private String xmlLocation; 

	@Bean
	public DataSource mysqlDataSource() {
        log.info(" ******************************************************************************************");
		log.info(" ===========================mysql数据库参数信息 start====================================="); 
		log.info("MySQLMybatisConfig Param driveClassName is :{}",driveClassName);
		log.info("MySQLMybatisConfig Param url is :{}",url);
		log.info("MySQLMybatisConfig Param userName is :{}",userName);
		log.info("MySQLMybatisConfig Param password is hide .");
		log.info("MySQLMybatisConfig Param xmlLocation is :{}",xmlLocation); 

		/** 1. 密码解密 ***/
		String dePassword = "";
		try {
			dePassword=this.decryptionPassword(password);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		// 更改默认验证方式，不使用 ping 模式验证
		System.setProperty("druid.mysql.usePingMethod","false");

		/** 2.参数设置 ***/
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setUrl(url);
		druidDataSource.setUsername(userName);
		druidDataSource.setPassword(dePassword);
		druidDataSource.setDriverClassName(driveClassName);
		// 初始化mysql数据源信息
		this.initDataSource(druidDataSource, DatabaseDialect.MYSQL);

        log.info(" ===========================mysql数据库参数信息 end=====================================");
        log.info(" ******************************************************************************************");
		return druidDataSource;
	}

	@Bean(name = "mysqlSqlSessionFactory")
	public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		/* 模糊匹配扫描包
		if (StringUtils.isNotBlank(typeAliasesPackage)) {
			bean.setTypeAliasesPackage(typeAliasesPackage);
		}
		*/
		// 添加XML目录
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		try {
			bean.setMapperLocations(resolver.getResources(xmlLocation));
			return bean.getObject();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Bean
	public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

	@Bean(name = "mysqlTransactionManager")
	public DataSourceTransactionManager transactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

    
    public String getUrl() {
        return url;
    }

    
    public void setUrl(String url) {
        this.url = url;
    }

    
    public String getUserName() {
        return userName;
    }

    
    public void setUserName(String userName) {
        this.userName = userName;
    }

    
    public String getPassword() {
        return password;
    }

    
    public void setPassword(String password) {
        this.password = password;
    }
	 
	
	
}


